package com.huanzhidadi.single.lambda;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * flatMap可以对stream流中单个元素再进行拆分（切片），从另一种角度上说，使用了它，就是使用了双重for循环。
 */
public class FlatMapTest {
    public static void main(String[] args) {
        List<MovieTrans> movieTransList = Arrays.asList(new MovieTrans("电影1"), new MovieTrans("电影2"));
        List<Movie> movieList = Arrays.asList(new Movie("已播放", movieTransList));
        // 这里的collect结果是List<List<MovieTrans>>类型，想要获取List<MovieTrans>需要使用flatMap
        //List<MovieTrans> collect = movieList.stream().map(item -> item.getMovieTransList()).collect(Collectors.toList());
        List<MovieTrans> collect = movieList.stream().flatMap(item -> item.getMovieTransList().stream()).collect(Collectors.toList());
        System.out.println(collect.size());
    }
}

@Data
@AllArgsConstructor
class Movie {
    private String example;
    private List<MovieTrans> movieTransList;
}

@Data
@AllArgsConstructor
class MovieTrans {
    private String text;

}